<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Orange框架</title>
    <meta name="generator" content="VuePress 1.9.10">
    <link rel="icon" href="/orange_doc/images/logo.jpg">
    <script>
            var _hmt = _hmt || [];
			(function() {
			  var hm = document.createElement("script");
			  hm.src = "https://hm.baidu.com/hm.js?a0c745d027acbd6848330c087669dc94";
			  var s = document.getElementsByTagName("script")[0]; 
			  s.parentNode.insertBefore(hm, s);
			})();
			</script>
    <meta name="description" content="orange框架,简洁高效的web开发框架, Golang用户极速开发体验。">
    <meta description="orange框架,简洁高效的web开发框架, Golang用户极速开发体验。">
    
    <link rel="preload" href="/orange_doc/assets/css/0.styles.4198b232.css" as="style"><link rel="preload" href="/orange_doc/assets/js/app.e323b7be.js" as="script"><link rel="preload" href="/orange_doc/assets/js/2.8ffd84cf.js" as="script"><link rel="preload" href="/orange_doc/assets/js/1.d3b0779f.js" as="script"><link rel="preload" href="/orange_doc/assets/js/44.324be1ea.js" as="script"><link rel="prefetch" href="/orange_doc/assets/js/10.d11338bf.js"><link rel="prefetch" href="/orange_doc/assets/js/11.03586221.js"><link rel="prefetch" href="/orange_doc/assets/js/12.8f3f5530.js"><link rel="prefetch" href="/orange_doc/assets/js/13.6489b7e0.js"><link rel="prefetch" href="/orange_doc/assets/js/14.3df437f3.js"><link rel="prefetch" href="/orange_doc/assets/js/15.be27f4ba.js"><link rel="prefetch" href="/orange_doc/assets/js/16.ea700694.js"><link rel="prefetch" href="/orange_doc/assets/js/17.a85dc572.js"><link rel="prefetch" href="/orange_doc/assets/js/18.359b700c.js"><link rel="prefetch" href="/orange_doc/assets/js/19.1ed550c9.js"><link rel="prefetch" href="/orange_doc/assets/js/20.067f2700.js"><link rel="prefetch" href="/orange_doc/assets/js/21.c8159272.js"><link rel="prefetch" href="/orange_doc/assets/js/22.3927f69e.js"><link rel="prefetch" href="/orange_doc/assets/js/23.64866326.js"><link rel="prefetch" href="/orange_doc/assets/js/24.405e9861.js"><link rel="prefetch" href="/orange_doc/assets/js/25.3f633c9f.js"><link rel="prefetch" href="/orange_doc/assets/js/26.26fdfa66.js"><link rel="prefetch" href="/orange_doc/assets/js/27.91b3d914.js"><link rel="prefetch" href="/orange_doc/assets/js/28.cd43e5e2.js"><link rel="prefetch" href="/orange_doc/assets/js/29.b587e0e0.js"><link rel="prefetch" href="/orange_doc/assets/js/3.36478a47.js"><link rel="prefetch" href="/orange_doc/assets/js/30.30aeda31.js"><link rel="prefetch" href="/orange_doc/assets/js/31.e61a62f2.js"><link rel="prefetch" href="/orange_doc/assets/js/32.124d2d7c.js"><link rel="prefetch" href="/orange_doc/assets/js/33.24b9dcd3.js"><link rel="prefetch" href="/orange_doc/assets/js/34.a5c00fc8.js"><link rel="prefetch" href="/orange_doc/assets/js/35.a7e238db.js"><link rel="prefetch" href="/orange_doc/assets/js/36.42c5fbe4.js"><link rel="prefetch" href="/orange_doc/assets/js/37.e014361d.js"><link rel="prefetch" href="/orange_doc/assets/js/38.5c677c29.js"><link rel="prefetch" href="/orange_doc/assets/js/39.d9324044.js"><link rel="prefetch" href="/orange_doc/assets/js/4.7e9344c8.js"><link rel="prefetch" href="/orange_doc/assets/js/40.b4486e63.js"><link rel="prefetch" href="/orange_doc/assets/js/41.daf98a13.js"><link rel="prefetch" href="/orange_doc/assets/js/42.4126b76f.js"><link rel="prefetch" href="/orange_doc/assets/js/43.d232e117.js"><link rel="prefetch" href="/orange_doc/assets/js/45.6d5645a4.js"><link rel="prefetch" href="/orange_doc/assets/js/46.8127c460.js"><link rel="prefetch" href="/orange_doc/assets/js/47.6a6a20c1.js"><link rel="prefetch" href="/orange_doc/assets/js/48.538157de.js"><link rel="prefetch" href="/orange_doc/assets/js/49.ab9e2921.js"><link rel="prefetch" href="/orange_doc/assets/js/5.45a5aec7.js"><link rel="prefetch" href="/orange_doc/assets/js/50.8ad7176f.js"><link rel="prefetch" href="/orange_doc/assets/js/6.43fcf2a3.js"><link rel="prefetch" href="/orange_doc/assets/js/7.569ec916.js"><link rel="prefetch" href="/orange_doc/assets/js/vendors~docsearch.9170ad82.js">
    <link rel="stylesheet" href="/orange_doc/assets/css/0.styles.4198b232.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/orange_doc/" class="home-link router-link-active"><img src="/orange_doc/images/logo.jpg" alt="Orange框架" class="logo"> <span class="site-name can-hide">Orange框架</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/orange_doc/" class="nav-link">
  首页
</a></div><div class="nav-item"><a href="/orange_doc/快速开始.html" class="nav-link">
  快速开始
</a></div><div class="nav-item"><a href="/orange_doc/about.html" class="nav-link">
  文档
</a></div><div class="nav-item"><a href="https://www.kancloud.cn/chase688/orange_framework" target="_blank" rel="noopener noreferrer" class="nav-link external">
  看云镜像
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://gitee.com/zhucheer/orange" target="_blank" rel="noopener noreferrer" class="nav-link external">
  源码
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/orange_doc/" class="nav-link">
  首页
</a></div><div class="nav-item"><a href="/orange_doc/快速开始.html" class="nav-link">
  快速开始
</a></div><div class="nav-item"><a href="/orange_doc/about.html" class="nav-link">
  文档
</a></div><div class="nav-item"><a href="https://www.kancloud.cn/chase688/orange_framework" target="_blank" rel="noopener noreferrer" class="nav-link external">
  看云镜像
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://gitee.com/zhucheer/orange" target="_blank" rel="noopener noreferrer" class="nav-link external">
  源码
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav>  <ul class="sidebar-links"><li><a href="/orange_doc/快速开始.html" class="sidebar-link">快速开始</a></li><li><a href="/orange_doc/config/配置.html" class="sidebar-link">配置</a></li><li><a href="/orange_doc/log/日志.html" class="sidebar-link">日志</a></li><li><a href="/orange_doc/config/命令行参数.html" class="sidebar-link">命令行参数</a></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>请求与响应</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/orange_doc/route/路由.html" class="sidebar-link">路由</a></li><li><a href="/orange_doc/route/静态资源绑定.html" class="sidebar-link">静态资源绑定</a></li><li><a href="/orange_doc/route/中间件.html" class="sidebar-link">中间件</a></li><li><a href="/orange_doc/controller/控制器.html" class="sidebar-link">控制器</a></li><li><a href="/orange_doc/request/Session.html" class="sidebar-link">Session</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>数据库</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/orange_doc/DB/Mysq操作.html" class="sidebar-link">Mysq操作</a></li><li><a href="/orange_doc/DB/Redis操作.html" class="sidebar-link">Redis操作</a></li></ul></section></li><li><a href="/orange_doc/view.html" class="sidebar-link">视图</a></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>其他</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/orange_doc/base/项目目录.html" class="sidebar-link">项目目录</a></li><li><a href="/orange_doc/base/项目编译.html" class="sidebar-link">项目编译</a></li><li><a href="/orange_doc/base/优雅退出.html" class="sidebar-link">优雅退出</a></li><li><a href="/orange_doc/base/平滑重启.html" class="sidebar-link">平滑重启</a></li><li><a href="/orange_doc/base/pprof支持.html" class="sidebar-link">pprof支持</a></li></ul></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>实用工具</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/orange_doc/tools/图片验证码.html" class="sidebar-link">图片验证码</a></li><li><a href="/orange_doc/tools/简单队列.html" class="sidebar-link">简单队列</a></li><li><a href="/orange_doc/tools/消息队列.html" class="sidebar-link">消息队列</a></li><li><a href="/orange_doc/tools/http请求客户端.html" class="active sidebar-link">http请求客户端</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/orange_doc/tools/http请求客户端.html#快速开始" class="sidebar-link">快速开始</a></li><li class="sidebar-sub-header"><a href="/orange_doc/tools/http请求客户端.html#post-请求" class="sidebar-link">POST 请求</a></li><li class="sidebar-sub-header"><a href="/orange_doc/tools/http请求客户端.html#更多配置" class="sidebar-link">更多配置</a></li><li class="sidebar-sub-header"><a href="/orange_doc/tools/http请求客户端.html#响应" class="sidebar-link">响应</a></li></ul></li><li><a href="/orange_doc/tools/文件上传.html" class="sidebar-link">文件上传</a></li><li><a href="/orange_doc/tools/发送邮件.html" class="sidebar-link">发送邮件</a></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><p>通常的业务需求都会有请求http服务的需求，包括数据爬虫、接口服务请求等等，该工具能提供一个高效易用的http请求客户端，支持 cookie 保持，能方便的实现模拟登陆请求，链式操作方便进行参数配置。</p> <h2 id="快速开始"><a href="#快速开始" class="header-anchor">#</a> 快速开始</h2> <p>通过如下方式，能快速发送一个 GET 请求</p> <div class="language- extra-class"><pre class="language-text"><code>client:=httpclient.NewClient()
resp,_:=client.RunGet(&quot;http://www.baidu.com&quot;)
</code></pre></div><h2 id="post-请求"><a href="#post-请求" class="header-anchor">#</a> POST 请求</h2> <p>通过如下方式，能快速发送一个 POST 请求</p> <div class="language- extra-class"><pre class="language-text"><code>client:=httpclient.NewClient()
resp,_:=client.RunPost(&quot;http://www.baidu.com&quot;)
</code></pre></div><p>通常情况下 POST 请求都会有请求参数，通过如下方式添加 POST 参数</p> <h3 id="方法1"><a href="#方法1" class="header-anchor">#</a> 方法1</h3> <p>使用 <code>FormParams</code> 方法配置一个 <code>map[string]interface{}</code> 参数；
默认 Orange 会以 <code>application/x-www-form-urlencoded</code> 的方式发送 POST 请求；
通过 <code>WithJsonRequest</code> / <code>WithFormRequest</code> 方法可以修改请求头中的文档类型；</p> <div class="language- extra-class"><pre class="language-text"><code>client:=httpclient.NewClient()
resp,_:=client.FormParams(map[string]interface{}{
   &quot;auth&quot;:123,
}).RunPost(&quot;http://127.0.0.1/test.php?s=1&quot;)

// 以json的方式发送请求，会将post转换成 {&quot;auth&quot;:123}
resp,_:=client.WithJsonRequest().FormParams(map[string]interface{}{
   &quot;auth&quot;:123,
}).RunPost(&quot;http://127.0.0.1/test.php?s=1&quot;)

// 以表单提交的方式发送请求
resp,_:=client.WithJsonRequest().FormParams(map[string]interface{}{
   &quot;auth&quot;:123,
}).RunPost(&quot;http://127.0.0.1/test.php?s=1&quot;)

</code></pre></div><h3 id="方法2"><a href="#方法2" class="header-anchor">#</a> 方法2</h3> <p>一种更为灵活的方法来发送 POST 参数；</p> <div class="language- extra-class"><pre class="language-text"><code>// 通过 ContentType 配置请求头文档类型
client:=httpclient.NewClient().ContentType(&quot;application/json&quot;)

// 通过 WithBody 添加请求 Body 中的信息
resp,_:=client.WithBody(`{&quot;auth&quot;:123}`).RunPost(&quot;http://127.0.0.1/test.php?s=1&quot;)
</code></pre></div><h2 id="更多配置"><a href="#更多配置" class="header-anchor">#</a> 更多配置</h2> <h3 id="cookie-保持"><a href="#cookie-保持" class="header-anchor">#</a> cookie 保持</h3> <p>通过 <code>WithCookie</code> 链式方法能像浏览器一样保持 cookie 解决模拟登陆抓包 session/cookie 问题；
注意：每一次调用 <code>httpclient.NewClient()</code> 相当于新创建一个浏览器，cookie 保持需要使用同一个 <code>httpclient.NewClient()</code> 返回的对象进行操作。</p> <div class="language- extra-class"><pre class="language-text"><code>client:=httpclient.NewClient().WithCookie()
resp,_: = client.RunGet(&quot;http://www.baidu.com&quot;)

resp,_ = client.RunGet(&quot;http://www.baidu.com/login&quot;)
</code></pre></div><h3 id="设置超时时间"><a href="#设置超时时间" class="header-anchor">#</a> 设置超时时间</h3> <p>Orange 框架中默认配置的请求超时时间是 10s 可以通过 <code>SetTimeout</code> 链式操作进行配置；</p> <div class="language- extra-class"><pre class="language-text"><code>// 设置 3s 的超时时间
client:=httpclient.NewClient().SetTimeout(3)
resp,_:=client.RunGet(&quot;http://www.baidu.com&quot;)
</code></pre></div><h3 id="设置请求头"><a href="#设置请求头" class="header-anchor">#</a> 设置请求头</h3> <p>通过 <code>Header</code> 链式操作配置请求头信息；</p> <div class="language- extra-class"><pre class="language-text"><code>client:=httpclient.NewClient().Header(&quot;auth&quot;, &quot;123&quot;)
resp,_:=client.RunGet(&quot;http://www.baidu.com&quot;)
</code></pre></div><h2 id="响应"><a href="#响应" class="header-anchor">#</a> 响应</h2> <p>Orange 请求接口返回的是一个响应结构体，里面包含原始响应数据，执行时间，等信息；</p> <div class="language- extra-class"><pre class="language-text"><code>client:=httpclient.NewClient()
resp,_:=client.RunGet(&quot;http://www.baidu.com&quot;)

// 获取原始 *http.Response
fmt.Println(resp.BodyRaw)

// 获取响应内容字符类型
fmt.Println(resp.String())

// 获取响应内容 byte 类型
fmt.Println(resp.Body)

// 获取 cookie 信息
fmt.Println(resp.CookieData)

// 获取请求执行时间
fmt.Println(resp.GetRequestTime())


</code></pre></div></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/orange_doc/tools/消息队列.html" class="prev">
        消息队列
      </a></span> <span class="next"><a href="/orange_doc/tools/文件上传.html">
        文件上传
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"></div></div>
    <script src="/orange_doc/assets/js/app.e323b7be.js" defer></script><script src="/orange_doc/assets/js/2.8ffd84cf.js" defer></script><script src="/orange_doc/assets/js/1.d3b0779f.js" defer></script><script src="/orange_doc/assets/js/44.324be1ea.js" defer></script>
  </body>
</html>
